package org.ztest.bean;

import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.BeanUtils;

public class BeanCopyTest
{
	private final int TEST_NUM = 100000;
	
	public void useSpring()
	{
		SourceBean sb = new SourceBean();
		sb.setA(1002);
		sb.setB("SSSSSSS");
		
		SourceBean sb2 = new SourceBean();
		
		long start = System.nanoTime();
		for (int i = 0; i < TEST_NUM; i++)
		{
			BeanUtils.copyProperties(sb, sb2);
		}
		System.out.println("Spring BeanUtils spent " + (System.nanoTime() - start) + "ns");
	}
	
	public void useApache() throws IllegalAccessException, InvocationTargetException
	{
		SourceBean sb = new SourceBean();
		sb.setA(1002);
		sb.setB("SSSSSSS");
		
		SourceBean sb2 = new SourceBean();
		
		long start = System.nanoTime();
		for (int i = 0; i < TEST_NUM; i++)
		{
			org.apache.commons.beanutils.BeanUtils.copyProperties(sb, sb2);
		}
		System.out.println("Apache BeanUtils spent " + (System.nanoTime() - start) + "ns");
	}
	
	public void directCopy()
	{
		SourceBean sb = new SourceBean();
		sb.setA(1002);
		sb.setB("SSSSSSS");
		
		SourceBean sb2 = new SourceBean();
		
		long start = System.nanoTime();
		for (int i = 0; i < TEST_NUM; i++)
		{
			sb2.setA(sb.getA());
			sb2.setB(sb.getB());
		}
		System.out.println("Apache BeanUtils spent " + (System.nanoTime() - start) + "ns");
	}
	
	class SourceBean
	{
		private int a;
		private String b;
		public int getA()
		{
			return a;
		}
		public void setA(int a)
		{
			this.a = a;
		}
		public String getB()
		{
			return b;
		}
		public void setB(String b)
		{
			this.b = b;
		}
	}
}
